//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdHestonBarrierEngine.h"
using namespace Cephei::QL::Pricingengines::Barrier;
#include <gen/QL/Models/Equity/HestonModel.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSchemeDesc.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL::Models::Equity;
using namespace Cephei::QL::Methods::Finitedifferences::Solvers;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::CFdHestonBarrierEngine (Cephei::QL::Models::Equity::IHestonModel^ model, Microsoft::FSharp::Core::FSharpOption<UInt64>^ tGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ xGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ vGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ dampingSteps, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc) : CPricingEngine(CFdHestonBarrierEngine::typeid)
{
    CHestonModel^ _Cmodel;
    CFdmSchemeDesc^ _CschemeDesc;
    try
    {
#ifdef HANDLE
        _phFdHestonBarrierEngine = NULL;
#endif
        _Cmodel = safe_cast<CHestonModel^> (model);
        _Cmodel->Lock();
        boost::shared_ptr<QuantLib::HestonModel>& _model = static_cast<boost::shared_ptr<QuantLib::HestonModel>&> (_Cmodel->GetShared ()); 
        QuantLib::Size _tGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (tGrid) ? (QuantLib::Size)ValueHelper::Convert (tGrid->Value) : 100); //4
        QuantLib::Size _xGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (xGrid) ? (QuantLib::Size)ValueHelper::Convert (xGrid->Value) : 100); //4
        QuantLib::Size _vGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (vGrid) ? (QuantLib::Size)ValueHelper::Convert (vGrid->Value) : 50); //4
        QuantLib::Size _dampingSteps = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (dampingSteps) ? (QuantLib::Size)ValueHelper::Convert (dampingSteps->Value) : 0); //4
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc))
        {
            _CschemeDesc = safe_cast<CFdmSchemeDesc^> (schemeDesc->Value);
            _CschemeDesc->Lock();
        }
        QuantLib::FdmSchemeDesc& _schemeDesc = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc) ? static_cast<QuantLib::FdmSchemeDesc&> (_CschemeDesc->GetReference ()) : QuantLib::FdmSchemeDesc::Hundsdorfer()); //1
        _ppFdHestonBarrierEngine = new boost::shared_ptr<QuantLib::FdHestonBarrierEngine> (new QuantLib::FdHestonBarrierEngine ( _model,  _tGrid,  _xGrid,  _vGrid,  _dampingSteps,  _schemeDesc ));
        SetPricingEngine (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdHestonBarrierEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmodel != nullptr) _Cmodel->Unlock();
        if (_CschemeDesc != nullptr) _CschemeDesc->Unlock();
    }
}
Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::CFdHestonBarrierEngine (boost::shared_ptr<QuantLib::FdHestonBarrierEngine>& childNative, Object^ owner) : CPricingEngine(CFdHestonBarrierEngine::typeid)
{
#ifdef HANDLE
	_phFdHestonBarrierEngine = NULL;
#endif
	_ppFdHestonBarrierEngine = &childNative;
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdHestonBarrierEngine));
}
Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::CFdHestonBarrierEngine (QuantLib::FdHestonBarrierEngine& childNative, Object^ owner) : CPricingEngine(CFdHestonBarrierEngine::typeid)
{
#ifdef HANDLE
	_phFdHestonBarrierEngine = NULL;
#endif
	_ppFdHestonBarrierEngine = new boost::shared_ptr<QuantLib::FdHestonBarrierEngine> (&childNative);
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdHestonBarrierEngine));
    _FdHestonBarrierEngineOwner = owner;
    _PricingEngineOwner = owner;
}

Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::CFdHestonBarrierEngine (CFdHestonBarrierEngine^ copy) : CPricingEngine(CFdHestonBarrierEngine::typeid)
{
#ifdef HANDLE
	_phFdHestonBarrierEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdHestonBarrierEngine = new boost::shared_ptr<QuantLib::FdHestonBarrierEngine> (copy->GetShared());
        _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdHestonBarrierEngine));
    }
}
Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::CFdHestonBarrierEngine (PLATFORM::Type^ t) : CPricingEngine(CFdHestonBarrierEngine::typeid)
{
#ifdef HANDLE
	_phFdHestonBarrierEngine = NULL;
#endif
	if (!t->IsSubclassOf(CFdHestonBarrierEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::CFdHestonBarrierEngine (QuantLib::Handle<QuantLib::FdHestonBarrierEngine>& childNative, Object^ owner)  : CPricingEngine(CFdHestonBarrierEngine::typeid)
{
	_phFdHestonBarrierEngine = &childNative;
	_ppFdHestonBarrierEngine = &static_cast<boost::shared_ptr<QuantLib::FdHestonBarrierEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdHestonBarrierEngine));
    _FdHestonBarrierEngineOwner = owner;
}
Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::CFdHestonBarrierEngine (QuantLib::Handle<QuantLib::FdHestonBarrierEngine> childNative)  : CPricingEngine(CFdHestonBarrierEngine::typeid)
{
	_phFdHestonBarrierEngine = &childNative;
	_ppFdHestonBarrierEngine = &static_cast<boost::shared_ptr<QuantLib::FdHestonBarrierEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdHestonBarrierEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::CFdHestonBarrierEngine (QuantLib::FdHestonBarrierEngine childNative)  : CPricingEngine(CFdHestonBarrierEngine::typeid)
{
#ifdef HANDLE
	_phFdHestonBarrierEngine = NULL;
#endif
	_ppFdHestonBarrierEngine = new boost::shared_ptr<QuantLib::FdHestonBarrierEngine> (new QuantLib::FdHestonBarrierEngine (childNative));
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdHestonBarrierEngine));
}
#endif

Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::~CFdHestonBarrierEngine ()
{
    if (_ppFdHestonBarrierEngine != NULL)
    {
	    delete _ppFdHestonBarrierEngine;
        _ppFdHestonBarrierEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::!CFdHestonBarrierEngine ()
{
    if (_ppFdHestonBarrierEngine != NULL)
    {
	    delete _ppFdHestonBarrierEngine;
    }
}
QuantLib::FdHestonBarrierEngine& Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::GetReference ()
{
    if (_ppFdHestonBarrierEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppFdHestonBarrierEngine;
}
boost::shared_ptr<QuantLib::FdHestonBarrierEngine>& Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::GetShared ()
{
    if (_ppFdHestonBarrierEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppFdHestonBarrierEngine;
}
QuantLib::FdHestonBarrierEngine* Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::GetPointer ()
{
    if (_ppFdHestonBarrierEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdHestonBarrierEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdHestonBarrierEngine>& Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::GetHandle ()
{
	if (_phFdHestonBarrierEngine == NULL)
	{
		_phFdHestonBarrierEngine = new Handle<QuantLib::FdHestonBarrierEngine> (*_ppFdHestonBarrierEngine);
	}
	return *_phFdHestonBarrierEngine;
}
#endif
bool Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::HasNative () 
{
	return (_ppFdHestonBarrierEngine != NULL);
}

Cephei::QL::Pricingengines::Barrier::IFdHestonBarrierEngine^ Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine::Calculate::get ()
{
    try
    {
    	(*_ppFdHestonBarrierEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Barrier::IFdHestonBarrierEngine^ Cephei::QL::Pricingengines::Barrier::CFdHestonBarrierEngine_Factory::Create (Cephei::QL::Models::Equity::IHestonModel^ model, Microsoft::FSharp::Core::FSharpOption<UInt64>^ tGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ xGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ vGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ dampingSteps, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc)
{
    return REFNEW CFdHestonBarrierEngine ( model,  tGrid,  xGrid,  vGrid,  dampingSteps,  schemeDesc);
}
